package org.kms.Controllers;

import org.kms.dao.fxinfoDao;
import org.kms.dao.selectedchargesDao;
import org.kms.dao.unselectedchargesDao;
import org.kms.pojo.Fxinfo;
import org.kms.pojo.Selectedcharges;
import org.kms.pojo.Unselectedcharges;
import org.kms.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Unselectedcharge")
@CrossOrigin(value = "*")
public class Unselectedcharge {
    @Autowired
    private selectedchargesDao selectedchargesDao;

    @Autowired
    private fxinfoDao fxinfoDao;

    @Autowired
    private unselectedchargesDao unselectedchargesDao;



    @RequestMapping("/unselectedInfo")
    @ResponseBody
    public ResponseResult<Map> getbycmidandpid(Integer cmId, Integer pid, Integer glId) throws Exception{
        Map<String,Object> map = new HashMap<>();
        List<Selectedcharges> selectedcharges = selectedchargesDao.getbycmIdpid(cmId,pid);
        List<Unselectedcharges> unselectedchargesList1 =unselectedchargesDao.getbyUncmIdpid(cmId,pid);
        if (cmId != null && pid == null && glId != null){
            List<Fxinfo> fxinfos =fxinfoDao.getbyfxinfos(glId);
            map.put("fxinfos",fxinfos);
        }
        if (selectedcharges.size() == 0 && unselectedchargesList1.size() ==0){
            List<Fxinfo> fxinfos =fxinfoDao.getbyfxinfos(glId);
            for (Fxinfo f: fxinfos
            ) {
                Unselectedcharges unselectedcharges = new Unselectedcharges();
                unselectedcharges.setPid(pid);
                unselectedcharges.setcIid(f.getcIid());
                unselectedchargesDao.add(unselectedcharges);
            }
        }
        if (cmId == null && pid == null){
            List<Fxinfo> fxinfos =fxinfoDao.getbyfxinfos(glId);
            map.put("fxinfos",fxinfos);
        }
        if (selectedcharges.size() > 0 && unselectedchargesList1.size() == 0){
            List<Integer> clids =fxinfoDao.getbynotclid(pid);
            for (int i = 0; i < clids.size(); i++) {
                Unselectedcharges unselectedcharges = new Unselectedcharges();
                unselectedcharges.setPid(pid);
                unselectedcharges.setcIid(clids.get(i));
                unselectedchargesDao.add(unselectedcharges);
            }
        }
        List<Selectedcharges> selectedchargesList = selectedchargesDao.getbycmIdpid(cmId,pid);
         List<Unselectedcharges> unselectedchargesList =unselectedchargesDao.getbyUncmIdpid(cmId,pid);
         map.put("selectedchargesList",selectedchargesList);
         map.put("unselectedchargesList",unselectedchargesList);

       return new ResponseResult<>(map);
    };

    @RequestMapping("/deleteUnselected")
    @ResponseBody
    public ResponseResult selectedadd(Selectedcharges selectedcharges,String pid1,String cIid1)throws Exception{
        String [] pid = pid1.split(",");
        String [] cIid = cIid1.split(",");
        for (int i = 0; i < pid.length; i++) {
            for (int j = 0; j < cIid.length; j++) {
                selectedcharges.setPid(Integer.parseInt(pid[i]));
                selectedcharges.setcIid(Integer.parseInt(cIid[j]));
                selectedchargesDao.add(selectedcharges);
                unselectedchargesDao.delgetbycIidAndpid(Integer.parseInt(cIid[j]),Integer.parseInt(pid[i]));
            }
        }
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/deleteselected")
    @ResponseBody
    public ResponseResult Unselectedadd(Unselectedcharges unselectedcharges,String pid1,String cIid1)throws Exception{
        String [] pid = pid1.split(",");
        String [] cIid = cIid1.split(",");
        for (int i = 0; i < pid.length; i++) {
            for (int j = 0; j < cIid.length; j++) {
                unselectedcharges.setPid(Integer.parseInt(pid[i]));
                unselectedcharges.setcIid(Integer.parseInt(cIid[j]));
                selectedchargesDao.delgetbycIidAndpid(Integer.parseInt(cIid[j]),Integer.parseInt(pid[i]));
                unselectedchargesDao.add(unselectedcharges);
            }
        }
        return ResponseResult.createSuccessResult();
    }


    @InitBinder
    public void InitBinder(WebDataBinder webDataBinder) throws Exception{
        //日期类型转换器
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor dateEditor = new CustomDateEditor(simpleDateFormat,true);
        //注册类型换换器:如果碰到Date.class 都会转成yyyy-MM-dd格式
        webDataBinder.registerCustomEditor(Date.class,dateEditor);
    }
}
